package me.jessyan.mvparms.demo.mvp.ui.activity.account;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.jessyan.mvparms.demo.R;
import me.jessyan.mvparms.demo.di.component.account.DaggerChangePhoneComponent;
import me.jessyan.mvparms.demo.di.module.account.ChangePhoneModule;
import me.jessyan.mvparms.demo.mvp.contract.account.ChangePhoneContract;
import me.jessyan.mvparms.demo.mvp.presenter.account.ChangePhonePresenter;
import me.jessyan.mvparms.demo.mvp.ui.widget.CustomToolbar;
import me.jessyan.mvparms.demo.mvp.ui.widget.MyVerifyLinerText;
import me.jessyan.mvparms.demo.mvp.ui.widget.PhoneEditText;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * 修改手机号
 */
public class ChangePhoneActivity extends BaseActivity<ChangePhonePresenter> implements ChangePhoneContract.View {

    @BindView(R.id.toolbar)
    CustomToolbar toolbar;
    @BindView(R.id.phone_et)
    PhoneEditText phoneEt;
    @BindView(R.id.verify_ll)
    MyVerifyLinerText verifyLl;
    @BindView(R.id.sure_tv)
    TextView sureTv;
    @BindView(R.id.verify_asking_tv)
    TextView verifyAskingTv;
    private boolean phoneOk, verifyOk;
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerChangePhoneComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .changePhoneModule(new ChangePhoneModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_change_phone; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        toolbar.setListener(new CustomToolbar.ToolbarListener() {
            @Override
            public void onLeftBack() {
                killMyself();
            }

            @Override
            public void onRightBack() {

            }
        });

        // 手机号输入框
        phoneEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //当12位时向后又输入一位达到加上空格十三位
                if (s.length() == 13) {
                    phoneOk = true;
                } else {
                    phoneOk = false;
                }
                checkLoginTvClickable();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        //验证码
        verifyLl.setVerifyListener(new MyVerifyLinerText.VerifyListener() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 4) {
                    verifyOk = true;
                } else {
                    verifyOk = false;
                }
                checkLoginTvClickable();
            }
            @Override
            public void onClick(View view) {
                verifyLl.startTimer();
            }
        });
        verifyAskingTv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
    }

    private void checkLoginTvClickable() {
    if(verifyOk&&phoneOk){
        sureTv.setEnabled(true);
    }else {
        sureTv.setEnabled(false);
    }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }



    @OnClick({R.id.sure_tv,R.id.verify_asking_tv})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.sure_tv:
                    finish();
                break;
            case R.id.verify_asking_tv:
                startActivity(new Intent(this,VerifyAskingActivity.class));
                break;
                default:
        }
    }
}
